package com.remainder.resource.controller;

import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Supplier;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.remainder.common.core.domain.R;
import com.remainder.common.core.enums.YN;
import com.remainder.common.core.utils.poi.ExcelUtil;
import com.remainder.common.core.web.controller.BaseController;
import com.remainder.common.core.web.domain.AjaxResult;
import com.remainder.common.core.web.page.TableDataInfo;
import com.remainder.common.log.annotation.Log;
import com.remainder.common.log.enums.BusinessType;
import com.remainder.common.security.annotation.RequiresPermissions;
import com.remainder.common.security.utils.SecurityUtils;
import com.remainder.resource.service.IUserService;
import com.remainder.sdk.export.config.ExportLogConfig;
import com.remainder.sdk.export.enums.ExportFileSourceEnum;
import com.remainder.sdk.export.service.BatchExport;
import com.remainder.system.api.domain.User;

/**
 * 用户Controller
 *
 * @author remainder
 * @date 2023-05-27
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {
    @Autowired
    private IUserService userService;
    @Resource
    private BatchExport<User> batchExportImpl;
    @Resource
    private Executor exportThreadPoolExecuter;

    /**
     * 查询用户列表
     */
    @RequiresPermissions("resource:user:list")
    @GetMapping("/list")
    public TableDataInfo list(User user) {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户列表
     */
    @RequiresPermissions("resource:user:export")
    @Log(title = "用户", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, User user) {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户数据");
    }

    /**
     * 获取用户详细信息
     */
    @RequiresPermissions("resource:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userService.selectUserById(id));
    }

    /**
     * 新增用户
     */
    @RequiresPermissions("resource:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody User user) {
        return toAjax(userService.insertUser(user));
    }

    /**
     * 批量新增用户
     */
    @RequiresPermissions("resource:user:add")
    @Log(title = "用户", businessType = BusinessType.INSERT)
    @PostMapping("/addBatchUser")
    public AjaxResult addBatch(@RequestBody User user) {
        return toAjax(userService.batchInsertUser(user));
    }

    /**
     * 修改用户
     */
    @RequiresPermissions("resource:user:edit")
    @Log(title = "用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody User user) {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户
     */
    @RequiresPermissions("resource:user:remove")
    @Log(title = "用户", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(userService.deleteUserByIds(ids));
    }

    /**
     * 异步批量导出
     */
    @PostMapping("/exportAsync")
    @RequiresPermissions("resource:user:exportAsync")
    public R<?> exportAsync(@RequestBody User user) {
        Long userId = SecurityUtils.getUserId();
        String username = SecurityUtils.getUsername();

        CompletableFuture.runAsync(() -> {
            Supplier<List<User>> supplier = () -> userService.selectUserList(user);
            ExportLogConfig config = new ExportLogConfig();
            config.setOperatorUserId(userId);
            config.setOperatorName(username);
            config.setExportFileSourceEnum(ExportFileSourceEnum.USER);
            config.setOperatingSource(YN.NO);
            batchExportImpl.startExport(config, supplier);
        }, exportThreadPoolExecuter);
        return R.ok();
    }
}
